package cn.edu.scnu.semart.goods.controller;

import cn.edu.scnu.semart.goods.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户点赞Controller
 */
@RestController
@RequestMapping("/like")
//@CrossOrigin
public class LikeController {

    @Autowired
    private LikeService likeService;

    /**
     * 点赞商品
     */
    @PostMapping("/goods")
    public Map<String, Object> likeGoods(
            @RequestParam Integer userId,
            @RequestParam Integer goodsId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = likeService.likeGoods(userId, goodsId);
            result.put("success", success);
            if (success) {
                result.put("message", "点赞成功");
            } else {
                result.put("message", "点赞失败，可能已经点赞过了");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 取消点赞商品
     */
    @PostMapping("/goods/unlike")
    public Map<String, Object> unlikeGoods(
            @RequestParam Integer userId,
            @RequestParam Integer goodsId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = likeService.unlikeGoods(userId, goodsId);
            result.put("success", success);
            if (success) {
                result.put("message", "取消点赞成功");
            } else {
                result.put("message", "取消点赞失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 点赞评论
     */
    @PostMapping("/comment")
    public Map<String, Object> likeComment(
            @RequestParam Integer userId,
            @RequestParam Integer commentId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = likeService.likeComment(userId, commentId);
            result.put("success", success);
            if (success) {
                result.put("message", "点赞成功");
            } else {
                result.put("message", "点赞失败，可能已经点赞过了");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 取消点赞评论
     */
    @PostMapping("/comment/unlike")
    public Map<String, Object> unlikeComment(
            @RequestParam Integer userId,
            @RequestParam Integer commentId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = likeService.unlikeComment(userId, commentId);
            result.put("success", success);
            if (success) {
                result.put("message", "取消点赞成功");
            } else {
                result.put("message", "取消点赞失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 检查是否已点赞商品
     */
    @GetMapping("/goods/check")
    public Map<String, Object> checkGoodsLike(
            @RequestParam Integer userId,
            @RequestParam Integer goodsId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            boolean isLiked = likeService.isLikedGoods(userId, goodsId);
            result.put("success", true);
            result.put("data", isLiked);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 检查是否已点赞评论
     */
    @GetMapping("/comment/check")
    public Map<String, Object> checkCommentLike(
            @RequestParam Integer userId,
            @RequestParam Integer commentId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            boolean isLiked = likeService.isLikedComment(userId, commentId);
            result.put("success", true);
            result.put("data", isLiked);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取商品点赞数
     */
    @GetMapping("/goods/count/{goodsId}")
    public Map<String, Object> getGoodsLikeCount(@PathVariable Integer goodsId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Integer count = likeService.getGoodsLikeCount(goodsId);
            result.put("success", true);
            result.put("data", count);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取评论点赞数
     */
    @GetMapping("/comment/count/{commentId}")
    public Map<String, Object> getCommentLikeCount(@PathVariable Integer commentId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Integer count = likeService.getCommentLikeCount(commentId);
            result.put("success", true);
            result.put("data", count);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
} 